package com.idroid.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by lisi on 2016/7/1.
 */
public class VideoRecordProgressBar extends View {
    private Paint mPaint;
    private int mBgColor;
    private int mProgressColor;
    private float mMax;
    private float mProgress;
    private int mWid;
    private int mHei;

    public VideoRecordProgressBar(Context context) {
        this(context, null);
    }

    public VideoRecordProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VideoRecordProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mPaint = new Paint();
        mBgColor = Color.BLACK;
        mProgressColor = Color.parseColor("#fe8026");
        mMax = 8;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画背景
        mPaint.setColor(mBgColor);
        mPaint.setAntiAlias(true);
        canvas.drawRect(0, 0, mWid, mHei, mPaint);


        //画进度
        mPaint.setColor(mProgressColor);
        int left = (int) ((mWid * mProgress / mMax) / 2);
        int right = mWid - left;
        canvas.drawRect(left, 0, right, getHeight(), mPaint);

    }

    public void setProgress(float mProgress) {
        if (mProgress < 0) {
            throw new IllegalArgumentException("progress not less than 0");
        }
        if (mProgress > mMax) {
            mProgress = mMax;
        }
        if (mProgress <= mMax) {
            this.mProgress = mProgress;
            invalidate();
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        mWid = getWidth();
        mHei = getHeight();
    }
}